#pragma once

#include <GL\glew.h>
#include <GL\glfw.h>

#include <vector>
#include <iostream>
#include <string>



//GL_INVALID_ENUM: given when an enumeration parameter contains an enum that is not allowed for that function.
//GL_INVALID_VALUE: given when a numerical parameter does not conform to the range requirements that the function places upon it.
//GL_INVALID_OPERATION: given when the function in question cannot be executed because of state that has been set in the context.
//GL_STACK_OVERFLOW: given when a stack pushing operation causes a stack to overflow the limit of that stack's size.
//GL_STACK_UNDERFLOW: given when a stack popping operation is given when the stack is already at its lowest point.
//GL_OUT_OF_MEMORY: given when performing an operation that can allocate memory, when the memory in question cannot be allocated.
//GL_TABLE_TOO_LARGE (if the optional imaging subset (GL_ARB_imaging) is supported)
//GL_INVALID_FRAMEBUFFER_OPERATION 

inline
bool checkGLErrors()
{
	bool retval = false;
	GLenum errCode;
	errCode = glGetError();
	if( errCode != GL_NO_ERROR)
	{
		retval = true;
		// Put each error in a list
		std::vector<std::string> errorList;
		do
		{
			//std::string msg = std::string( (char*)gluErrorString(errCode) );
			std::string msg = "";
			switch( errCode )
			{
			case GL_INVALID_ENUM:
				msg = "GL_INVALID_ENUM, did you type GL_BEGIN(GL_LINE), instead of GL_LINES?";
				break;
			case GL_INVALID_VALUE:
				msg = "GL_INVALID_VALUE, did you pass float where and int was expected?";
				break;
			case GL_INVALID_OPERATION:
				msg = "GL_INVALID_OPERATION";
				break;
			case GL_STACK_OVERFLOW:
				msg = "GL_STACK_OVERFLOW, stray push?";
				break;
			case GL_STACK_UNDERFLOW:
				msg = "GL_STACK_UNDERFLOW, stray pop?";
				break;
			case GL_OUT_OF_MEMORY:
				msg = "GL_OUT_OF_MEMORY";
				break;
			case GL_TABLE_TOO_LARGE:
				msg = "GL_TABLE_TOO_LARGE";
				break;
			case GL_INVALID_FRAMEBUFFER_OPERATION:
				msg = "GL_INVALID_FRAMEBUFFER_OPERATION";
				break;
			default:
				msg = "unknow gl error";
				break;
			}

			errorList.push_back( msg );
			errCode = glGetError();
		}while (errCode != GL_NO_ERROR);

		std::cout << "num gl errors this frame: " << errorList.size() << std::endl;
		
		for(size_t i=0; i<errorList.size(); i++)
		{
			std::cout << i << " : " << errorList[i] << std::endl;
		}
		
	}
	return retval;
}